// 文件名：lc337-欧琳琳.cpp
// 提交链接：https://leetcode.cn/problems/house-robber-iii/submissions/
// 337. 打家劫舍 III - 中等题
// 完成日期：2024/8/31
// c++, 树，深度优先搜索，动态规划，二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> robHelper(TreeNode* root)
    {
        if (!root) return {0, 0};
        vector<int> left=robHelper(root->left);
        vector<int> right=robHelper(root->right);
        int rob_current=root->val+left[0]+right[0];
        int not_rob_current=max(left[0],left[1])+max(right[0],right[1]);
        return {not_rob_current,rob_current};//robHelper[0]是不算当前节点的，robHelper[1]是算当前节点的  
    }
    int rob(TreeNode* root) {
        vector<int> a=robHelper(root);
        return max(a[0],a[1]);
    }
};